package com.batman.baselibrary.widget.update;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.batman.baselibrary.R;

import org.lzh.framework.updatepluginlib.base.DownloadCallback;
import org.lzh.framework.updatepluginlib.base.DownloadNotifier;
import org.lzh.framework.updatepluginlib.model.Update;

import java.io.File;
import java.util.UUID;

/**
 * 作者：yuhb on 2019/7/14 22:34.
 * 描述：
 */
public class NotificationDownloadCreator extends DownloadNotifier {


    @Override
    public DownloadCallback create(Update update, Activity activity) {
        // 返回一个UpdateDownloadCB对象用于下载时使用来更新界面。
        return new NotificationCB(activity);
    }

    private static class NotificationCB implements DownloadCallback {

        NotificationManager manager;
        NotificationCompat.Builder builder;


        Notification notification = null;


        int id;
        int preProgress;

        NotificationCB(Activity activity) {
            this.manager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
            builder = new NotificationCompat.Builder(activity);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel("my_channel_01", "ic_cer_bg",
                        NotificationManager.IMPORTANCE_LOW);
                manager.createNotificationChannel(mChannel);
                builder = new NotificationCompat.Builder(activity, "my_channel_01")
                        .setProgress(100, 0, false)
                        .setSmallIcon(R.drawable.ic_small_notify)//设置通知小ICON
                        .setAutoCancel(false)
                        .setTicker("开始下载")
                        .setContentTitle("双重安培")
                        .setContentText("开始下载");
                notification = builder.build();

            } else {
                builder = new NotificationCompat.Builder(activity);
                builder.setProgress(100, 0, false)
                        .setSmallIcon(R.drawable.ic_small_notify)//设置通知小ICON
                        .setAutoCancel(false)
                        .setTicker("开始下载")
                        .setContentTitle("双重安培")
                        .setContentText("开始下载");

                notification = builder.build();
            }

            id = Math.abs(UUID.randomUUID().hashCode());
        }

        @Override
        public void onDownloadStart() {
            // 下载开始时的通知回调。运行于主线程
            manager.notify(id, notification);
        }

        @Override
        public void onDownloadComplete(File file) {
            // 下载完成的回调。运行于主线程
            manager.cancel(id);
        }

        @Override
        public void onDownloadProgress(long current, long total) {
            // 下载过程中的进度信息。在此获取进度信息。运行于主线程
            int progress = (int) (current * 1f / total * 100);
            // 过滤不必要的刷新进度
            if (preProgress < progress) {
                preProgress = progress;
                builder.setProgress(100, progress, false);
                notification = builder.build();
                manager.notify(id, notification);
            }
        }

        @Override
        public void onDownloadError(Throwable t) {
            // 下载时出错。运行于主线程
            manager.cancel(id);
        }
    }


}
